package org.jeecg.modules.bus.controller;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hpsf.Decimal;
import org.jeecg.annotation.ApiLogin;
import org.jeecg.com.util.ThirdSessionHolder;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TenantContextHolder;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.ApiVersion;
import org.jeecg.config.ApiVersionConstant;
import org.jeecg.config.WebConfig;
import org.jeecg.modules.business.dto.CouponsGenerateDto;
import org.jeecg.modules.business.dto.PostDataDto;
import org.jeecg.modules.business.dto.ProvideCouponsUsedDto;
import org.jeecg.modules.business.entity.*;
import org.jeecg.modules.business.enums.CouponsStatusEnum;
import org.jeecg.modules.business.service.IBusMarketCouponsCashService;
import org.jeecg.modules.business.service.IBusMarketCouponsCashUsedService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.business.service.IBusMarketCouponsService;
import org.jeecg.modules.business.service.IBusMarketCouponsUsedService;
import org.jeecg.modules.mall.entity.MallUserInfo;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.wxuser.service.IUserInfoService;
import org.jeecg.modules.wxuser.service.impl.UserInfoServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;


@Api(tags="优惠券")
@RestController
@AllArgsConstructor
@RequestMapping("/mall-api/coupons-cash-used")
@Slf4j
public class CouponsCashUsedController extends WebConfig {
    @Resource
    private IBusMarketCouponsCashUsedService busMarketCouponsCashUsedService;
    @Resource
    private IBusMarketCouponsService busMarketCouponsService;
    @Resource
private IBusMarketCouponsCashService busMarketCouponsCashService;
    @Resource
    private IUserInfoService userInfoService;
    /**
     * 获取我的可用优惠券分页列表查询
     *
     * @param busMarketCouponsCashUsed
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "获取我的优惠券分页列表查询", notes = "获取我的优惠券分页列表查询")
    @GetMapping(value = "/list")
    @ApiLogin
    @ApiVersion(group = ApiVersionConstant.FAP_MALLAPI101)
    public Result<IPage<BusMarketCouponsCashUsed>> queryPageList(BusMarketCouponsCashUsed busMarketCouponsCashUsed,
                                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                 HttpServletRequest req) {
        String tenantId = TenantContextHolder.getTenantId();
        String userId = ThirdSessionHolder.getUserId();
        MallUserInfo mallUserInfo = userInfoService.getById(userId);
        if (StringUtils.isNotBlank(mallUserInfo.getMobile())) {
            LambdaQueryWrapper<BusMarketCouponsCashUsed> queryWrapper = QueryGenerator.initQueryWrapper(busMarketCouponsCashUsed, req.getParameterMap()).lambda();
            queryWrapper.eq(BusMarketCouponsCashUsed::getMobile, mallUserInfo.getMobile());
            queryWrapper.eq(BusMarketCouponsCashUsed::getTenantId, tenantId);
            queryWrapper.eq(BusMarketCouponsCashUsed::getStatus, 1);
            Page<BusMarketCouponsCashUsed> page = new Page<BusMarketCouponsCashUsed>(pageNo, pageSize);
            IPage<BusMarketCouponsCashUsed> pageList = busMarketCouponsCashUsedService.page(page, queryWrapper);
            pageList.getRecords().forEach(item -> {
                BusMarketCouponsCash coupons = busMarketCouponsCashService.getById(item.getCouponsId());
                if (coupons != null) {
                    item.setBusMarketCouponsCash(coupons);
                }
                CouponsStatusEnum couponsStatusEnum = CouponsStatusEnum.val(item.getStatus());
                if (couponsStatusEnum != null) {
                    item.setStatusName(couponsStatusEnum.getTitle());
                }
            });
            return Result.OK(pageList);
        }
        return Result.OK(null);
    }
}
